﻿#region Usings

using PolyGameEngine.GamePieces;
using PolyGameEngine.GamePieces.Common;
using PolyGameEngine.Tools;

using FarseerPhysics.Dynamics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace PolyMorph.CustomGamePieces
{
    public class SensorForceTile : ForceTile
    {
        protected CircleUnit AffectedGamePiece = null;
        int timer = 0;
        int time = 0;

        public SensorForceTile(PhysicsGame PhysicsGame, World World, Material Material,
            Texture2D SpriteSheet, int AnimationSpeed, Vector2 Position, float Rotation, float strenght, float DrawingDepth, int saveLineIndex, string GPID, float effectLengthSeconds)
            : base(PhysicsGame, World, Material, SpriteSheet, AnimationSpeed, Position, Vector2.Zero, Rotation, strenght, DrawingDepth, saveLineIndex, GPID)
        {
            body.IsSensor = true;
            time = (int)(effectLengthSeconds * 60);
        }

        public override void Update(GameTime gameTime)
        {
            if (AffectedGamePiece != null)
            {
                DuringEffect();
                timer--;
            }

            if (timer <= 0)
            {
                EndEffect();
            }

            base.Update(gameTime);
        }

        public virtual void StartEffect()
        {
            timer = time;
        }

        public virtual void DuringEffect()
        {
        }

        public virtual void EndEffect()
        {
            AffectedGamePiece = null;
        }


        public override void HandleDefaultAfterEffect(Fixture ThisFixture, Fixture OtherFixture)
        {
            if (GamePieceInEffect!=null&&GamePieceInEffect.GetType().Equals(typeof(CircleUnit)))
            {
                AffectedGamePiece = (CircleUnit)GamePieceInEffect;
                StartEffect();

            }

            base.HandleDefaultAfterEffect(ThisFixture, OtherFixture);
        }

    }
}


